<?php
/**
 * Created by PhpStorm.
 * User: 帅帅吴
 * Date: 2018/11/16
 * Time: 9:55
 */

namespace app\admin\controller;

//use app\admin\model\Card as CardModel;
//use app\admin\model\CardUserStock as CardUserStockModel;
//use app\admin\model\CardRule as CardRuleModel;
//use app\admin\model\Gift as GiftModel;
//use app\admin\model\Vehicle as VehicleModel;
//use app\admin\model\CardIncordec;
class Test extends Index
{
    /**
     * @return \think\response\View
     * @user shuaishuaiwu
     * @date 2018/11/16
     * @time 9:59
     * @title 卡片内容
     */
    public function testIndex()
    {
        return json(['code'=>200,'msg'=>'testIndex']);
        //return view();
    }

    /**
     * @return \think\response\Json
     * @user shuaishuai wu
     * @date 2018/11/16
     * @time 10:09
     * @title 卡片内容数据
     */
    public function testIndexData()
    {
        return json(['code'=>200,'msg'=>'testIndexData']);
    }

    /**
     * @user shuaishuaiwu
     * @date 2018/11/16
     * @time 12:52
     * @title 卡片添加
     */
    public function testAdd()
    {
       return json(['code'=>200,'msg'=>'testAdd']);
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/11/16
     * @time 12:53
     * @title 卡片上架下架
     */
    public function changeStatus()
    {
        return json(['code'=>200,'msg'=>'changeStatus']);
    }

    /**
     * @return \think\response\View
     * @user 帅帅吴
     * @date 2018/11/16
     * @time 15:32
     * @title 用户卡片
     *
     */
    public function cardUserStockIndex()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * @user 帅帅吴
     * @date 2018/11/16
     * @time 15:33
     * @title 用户卡片数据
     */
    public function cardUserStockIndexData()
    {
        $cardUserStockModel = new CardUserStockModel();
        $data = $cardUserStockModel->getDataList();
        return json($data);
    }

    /**
     * @return $this
     * @user shguaishuaiwu
     * @date 2018/11/16
     * @time 15:35
     * @title 用户卡片详清
     */
    public function cardUserStockDetail()
    {
        $cardUserStockModel = new CardUserStockModel();
        $data = $cardUserStockModel->cardUserStockDetail();
//        $this->assign('data',$data);
//        $this->fetch();  //这种方法有毒,千万别用
        return view()->assign('data',$data);
    }

    /**
     * @return \think\response\View
     * @user shuaishuai wu
     * @date 2018/11/16
     * @time 15:48
     * @title 卡片合成规则表
     */
    public function cardRuleIndex()
    {
        $giftModel = new GiftModel();
        $vehicleModel = new VehicleModel();
        $cardModel = new CardModel();
        $giftData =  $giftModel->getDataList();
        $vehicleData = $vehicleModel->getDataList();
        $cardData = $cardModel->getDataList();
        $this->assign(['giftData' =>$giftData,'vehicleData' =>$vehicleData,'cardData'=>$cardData]);
        return view();
    }

    /**
     * @return \think\response\Json
     * @user shuaishuai wu
     * @date 2018/11/16
     *@time 16:24
     *@title 卡片合成规则
     */
    public function cardRuleIndexData()
    {
        $cardRuleModel = new CardRuleModel();
        $data = $cardRuleModel->getDataList();
        return json($data);
    }

    /**
     * @return array|\think\response\Redirect
     * @user shuaishuai wu
     * @date 2018/11/27
     * @time 14:14
     * @title 卡片合成礼物筛选
     */
    public function cardRuleGiftName(){
        $vehicleModel = new VehicleModel();
        $giftModel = new GiftModel();
        if(!empty(input('post.type'))){
            $type = input('post.type');
            $return = [];
            switch($type){
                case 1:
                    $returnData = $vehicleModel->field('vehicle_id,vehicle_name')->select();
                    foreach($returnData as $key =>$value){
                        $return[$key]['id'] = $value['vehicle_id'];
                        $return[$key]['gift_name'] = $value['vehicle_name'];
                    }
                    break;
                case 2:
                    $returnData = $giftModel->field('id,gift_name')->select();
                    foreach($returnData as $key =>$value){
                        $return[$key]['id'] = $value['id'];
                        $return[$key]['gift_name'] = $value['gift_name'];
                    }
                    break;
            }
            return $return;
        }
        return redirect('/Miss');
    }
    /**
     * @return array
     * @user shuaishuai wu
     * @date 20108/1/17
     * @time 15:15
     * @title 禁止合成,开放合成
     */
    public function cardRuleChangeStatus()
    {
        if(request()->isGet()){
            $cardRuleModel = new CardRuleModel();
            $cardDetail = $cardRuleModel->where('id','=',input('get.id'))->find();
            $newData['status'] = $cardDetail['status'] === 1 ? 0 : 1;
            try{
                $cardRuleModel->save($newData,['id'=>input('get.id')]);
                return ['code'=>200,'msg'=>'修改成功'];
            }catch(\Exception $e){
                $this->error(lang('edit').lang('false').$e->getMessage());
            }
        }
        return redirect('/Miss');
    }
    /**
     * @user shuaishuaiwu
     * @date 2018/11/17
     * @time 14:32
     * @title 添加合成卡片
     */
    public function cardRuleAdd()
    {
        if(request()->isPost()){
            $newData['card_id'] = input('post.card_id');
            $newData['card_num'] = input('post.card_num');
            $newData['status'] = input('post.status');
            if(!empty(input('type_id')) && !empty(input('typed'))){
                $newData['type'] = input('typed');
                $newData['type_id'] = input('type_id');
                try{
                    $cardRuleModel = new CardRuleModel();
                    $cardRuleModel->save($newData);
                }catch(\Exception $e){
                    $this->error(lang('add').lang('false').$e->getMessage());
                }
                $this->success(lang('add').lang('success'));
            }
        }
        return redirect('/Miss');
    }
    /**
     * @user shuaishuai wu
     * @date 2018/11/19
     * @time 14:05
     * @title 修改卡片的合成条件
     */
    public function cardRuleEdit()
    {
        if(!empty(input('get.rule_id'))){
            $cardRuleModel = new CardRuleModel();
            $newData['id'] = input('get.rule_id');
            $newData['card_num'] = input('get.card_num');
            $newData['status'] = input('get.status');
            $newData['type_id'] = input('get.type_id');
            $newData['type'] = 2;
            if( empty($newData['card_num']) || !isset($newData['status']))
                return ['code'=>-100,'msg'=>'请填写完整'];
            if(empty($newData['type_id']) && empty(input('typed_id')))
                return ['code'=>-100,'msg'=>'请选择一个奖品'];
            if(!empty(input('typed_id'))){
                $newData['type_id'] = input('typed_id');
                $newData['type'] = 1;
            }
            try{
                $cardRuleModel->save($newData,['id'=> $newData['id']]);
            }catch(\Exception $e){
                return ['code'=>-100,'msg'=>'修改失败'];
            }
            return ['code'=>100,'msg'=>'修改成功'];
        }else{
            $id = input('get.id');
            $giftModel = new GiftModel();
            $vehicleModel = new VehicleModel();
            $giftData =  $giftModel->getDataList();
            $vehicleData = $vehicleModel->getDataList();
            $this->assign(['giftData' =>$giftData,'vehicleData' =>$vehicleData,'id'=>$id]);
            return view();
        }
    }

    /**
     * @return \think\response\View
     * @user shuaishuai wu
     * @date 2018/11/27
     * @time 11:34
     * @title 卡片收支
     */
    public function inoutIndex()
    {
        return view();

    }
    /**
     * @return \think\response\View
     * @user shuaishuai wu
     * @date 2018/11/27
     * @time 11:34
     * @title 卡片收支数据
     */
    public function inoutIndexData()
    {
        $cardIncordecModel = new CardIncordec();
        $data = $cardIncordecModel->getDataList();
        return json($data);
    }

    /**
     * @return array|\think\response\Redirect
     * @user 帅帅 wu
     * @date 2018/11/27
     * @time 14:55
     * @title 同意申请黑卡兑换
     */
    public function inoutAgree()
    {
       if(!empty(input('id'))){
            $id = input('id');
            $newData['auditing'] = 2;
           $cardIncordecModel = new CardIncordec();
          $res = $cardIncordecModel->save($newData,['id'=>$id]);
          if($res){
              return ['code'=>200,'msg'=>'同意成功'];
          }else{
              return ['code'=>-200,'msg'=>'同意失败'];
          }
       }
       return redirect('/Miss');
    }

    /**
     * @return array|\think\response\Redirect
     * @user shuaishuai wu
     * @time 14;58
     * @date 2018/11/27
     * @title 拒绝申请黑卡兑换
     */
    public function inoutRefuse()
    {
        if(!empty(input('id'))){
            $id = input('id');
            $newData['status'] = 3;
            $cardIncordecModel = new CardIncordec();
            $res = $cardIncordecModel->save($newData,['id'=>$id]);
            if($res){
                return ['code'=>200,'msg'=>'拒绝成功'];
            }else{
                return ['code'=>-200,'msg'=>'拒绝失败'];
            }
        }
        return redirect('/Miss');
    }

    /**
     * @return $this
     * @user suhaisuhiai
     * @date 2018/12/5
     * @time 10:09
     * @title 添加礼袋礼物
     */
    public function cardAddGift()
    {
        $user_id = input('get.user_id');
        $incordec_id = input('get.incordec_id');

        if(input('get.gift_id')){

            $redis = new \Redis();
            $redis->connect(config('app.redis.host'), 6379);
            $redis->auth(config('app.redis.pwd'));

            $user_black = json_decode($redis->get('user_black'),true);  //获取总缓存
            $user_black[$user_id][$incordec_id]['gift_id'] = input('get.gift_id');
            $res = $redis->set('user_black',json_encode($user_black));
            if($res){
                $newData['auditing'] = 2;
                $newData['incorde_id'] = input('get.gift_id');
                $cardIncordecModel = new CardIncordec();
                $cardIncordecModel->save($newData,['id'=>$incordec_id]);
                return ['code'=>100,'msg'=>'添加成功'];
            }
            else
                return ['code'=>100,'msg'=>'添加失败'];
        }
        else
            return view()->assign(['user_id'=>$user_id,'incordec_id'=>$incordec_id]);
    }


}